
const USER_INFO_KEY = 'vue-shopping-centre_user_info'
const SEARCH_HISTORY_KEY = 'vue-shopping-centre_search_history'
const ORDER_ITEM_TMP_KEY = 'vue-shopping-centre_order_item_tmp'

export const GetUserInfoFromLocal = () => {
  let obj = { token: '', userId: '' }
  const result = localStorage.getItem(USER_INFO_KEY)
  if (result) {
    obj = JSON.parse(result)
  }

  return obj
}

export const SaveUserInfoToLocal = (info) => {
  const str = JSON.stringify(info)
  localStorage.setItem(USER_INFO_KEY, str)
}

export const RmUserInfoInLocal = () => {
  localStorage.removeItem(USER_INFO_KEY)
}

// search history
export const GetSearchHistoryFromLocal = () => {
  let obj = []
  const result = localStorage.getItem(SEARCH_HISTORY_KEY)
  if (result) {
    obj = JSON.parse(result)
  }

  return obj
}

export const SaveSearchHistoryToLocal = (historyList) => {
  const str = JSON.stringify(historyList)
  localStorage.setItem(SEARCH_HISTORY_KEY, str)
}

export const GetOrderItemTmpFromLocal = () => {
  let orderItemTmp = {
    cartGoodsList: [],
    deliveryType: 10,
    addr: {},
    createdTime: '',
    status: 10 // 10 - 未付款, 20 - 已付款, 30 - 已收货
  }
  const result = localStorage.getItem(ORDER_ITEM_TMP_KEY)
  if (result) {
    let parseRet
    try {
      parseRet = JSON.parse(result)
    } catch {}
    if (typeof parseRet === 'object') {
      orderItemTmp = parseRet
    }
  }

  return orderItemTmp
}

export const SaveOrderItemTmpToLocal = (orderItemTmp) => {
  const str = JSON.stringify(orderItemTmp)
  localStorage.setItem(ORDER_ITEM_TMP_KEY, str)
}
